<script setup lang="ts">
import { computed } from 'vue';
import { Handle, Position, useVueFlow } from '@vue-flow/core';

interface TenantData {
  id: number;
  tenantName: string;
  tenantCode: string;
  status: string;
  contactName?: string;
}

interface Props {
  data: TenantData;
}

const props = defineProps<Props>();
const { getEdges } = useVueFlow();

// 检查是否已有映射关系
const isMapped = computed(() => {
  const edges = getEdges.value;
  return edges.some(edge => edge.source === `tenant-${props.data.id}`);
});
</script>

<template>
  <div class="tenant-node" :class="{ 'is-mapped': isMapped }">
    <div class="node-header">
      <div class="node-icon">
        <icon-ic-round-business class="text-16px" />
      </div>
      <div class="node-title">{{ data.tenantName }}</div>
      <div class="node-status">
        <ATag :color="data.status === '1' ? 'green' : 'orange'" size="small">
          {{ data.status === '1' ? '启用' : '禁用' }}
        </ATag>
      </div>
    </div>
    <div class="node-content">
      <div class="node-info">
        <div class="info-item">
          <span class="label">编码:</span>
          <span class="value">{{ data.tenantCode }}</span>
        </div>
        <div class="info-item">
          <span class="label">联系人:</span>
          <span class="value">{{ data.contactName || '-' }}</span>
        </div>
      </div>
      <div class="node-actions"></div>
    </div>
    <Handle :id="`tenant-${data.id}`" type="source" :position="Position.Right" class="tenant-handle" />
  </div>
</template>

<style scoped>
.tenant-node {
  background: #fff;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  padding: 12px;
  min-width: 200px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.2s;
}

/* 新增暗色模式样式 */
.dark .tenant-node {
  background: #1e293b;
  border-color: #334155;
  color: #f8fafc;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.tenant-node:hover {
  border-color: #1890ff;
  box-shadow: 0 4px 8px rgba(24, 144, 255, 0.15);
}

.tenant-node.is-mapped {
  border-color: #52c41a;
  background: linear-gradient(135deg, #f6ffed 0%, #ffffff 100%);
}

/* 新增暗色模式样式 */
.dark .tenant-node.is-mapped {
  border-color: #36d399;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
}

.node-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.node-icon {
  color: #1890ff;
}

.node-title {
  font-weight: 600;
  font-size: 14px;
  color: #262626;
  flex: 1;
}

/* 新增暗色模式样式 */
.dark .node-title {
  color: #f8fafc;
}

.node-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.node-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
}

.info-item .label {
  color: #8c8c8c;
}

.info-item .value {
  color: #262626;
  font-weight: 500;
}

/* 新增暗色模式样式 */
.dark .info-item .label {
  color: #94a3b8;
}

.dark .info-item .value {
  color: #e2e8f0;
}

.node-actions {
  display: flex;
  justify-content: center;
}

.tenant-handle {
  width: 8px;
  height: 8px;
  background: #1890ff;
  border: 2px solid #fff;
  box-shadow: 0 0 0 2px #1890ff;
}

.tenant-handle:hover {
  background: #40a9ff;
  box-shadow: 0 0 0 2px #40a9ff;
}
</style>
